#!/bin/bash

######## partition layout
# /dev/mmcblk3p1		Linux bootimage
# /dev/mmcblk3p2		Android bootimage
# /dev/mmcblk3p3
# /dev/mmcblk3p4		Linux/Android internal virtual sdcard
# /dev/mmcblk3p5		Android recovery
# /dev/mmcblk3p6		Linux rootfs
# /dev/mmcblk3p7		Linux appfs
# /dev/mmcblk3p8		Android data
# /dev/mmcblk3p9		Android system
# /dev/mmcblk3p10		Android cache
# /dev/mmcblk3p11		Android device
# /dev/mmcblk3p12		Android misc
# /dev/mmcblk3p13		Android data footer
# /dev/mmcblk3p14		Android meta data


# partition size in MB
UBOOT_SIZE=12		# uboot(4M) + logo(8M)
LBOOT_SIZE=16
ABOOT_SIZE=16
RBOOT_SIZE=16
LROOT_SIZE=256
LAPPS_SIZE=128

IDISK_SIZE=

DATA_SIZE=3072
SYSTEM_SIZE=1536
CACHE_SIZE=512
MISC_SIZE=4
DEVICE_SIZE=8

DATAFOOTER_SIZE=2
METADATA_SIZE=2

help() {

bn=`basename $0`
cat << EOF
usage $bn <option> device_node

options:
  -h				displays this help message
  -s				only get partition size
EOF

}

# parse command line
moreoptions=1
node="na"
cal_only=0
while [ "$moreoptions" = 1 -a $# -gt 0 ]; do
	case $1 in
	    -h) help; exit ;;
	    -s) cal_only=1 ;;
	    *)  moreoptions=0; node=$1 ;;
	esac
	[ "$moreoptions" = 0 ] && [ $# -gt 1 ] && help && exit
	[ "$moreoptions" = 1 ] && shift
done

if [ ! -e ${node} ]; then
	help
	exit
fi


# get sfdis version in PC
# sfdisk_version=`sfdisk -v | awk '{print $3}' | awk -F '.' '{print $2}'`

# get sfdis version in ARM board
sfdisk_version=`sfdisk -v | awk '{print $4}' | awk -F '.' '{print $2}'`

if [ $sfdisk_version -ge "26" ]; then
     opt_unit=""
     unit_mb="M"
else
     opt_unit="-uM"
     unit_mb=""
fi

# call sfdisk to create partition table
# get total card size
seprate=40
total_size=`sfdisk -s ${node}`
total_size=`expr ${total_size} / 1024`
boot_sizeb=`expr ${LBOOT_SIZE} + ${UBOOT_SIZE}`

extend_size=`expr ${RBOOT_SIZE} + ${LROOT_SIZE} + \
				  ${LAPPS_SIZE} + ${CACHE_SIZE} + \
				  ${SYSTEM_SIZE} + ${CACHE_SIZE} + \
				  ${MISC_SIZE} + ${DEVICE_SIZE} + \
				  ${DATAFOOTER_SIZE} + ${METADATA_SIZE} + \
				  ${DATA_SIZE} + ${seprate}`

disk_size=`expr ${total_size} - ${boot_sizeb} - \
				${ABOOT_SIZE} - ${extend_size}`


# show partitions
cat << EOF
========= partitions ========
UBOOT      ${node}    : ${UBOOT_SIZE}MB
LBOOT      ${node}p1  : ${LBOOT_SIZE}MB
ABOOT      ${node}p2  : ${ABOOT_SIZE}MB
IDISK      ${node}p4  : ${disk_size}MB
RBOOT      ${node}p5  : ${RBOOT_SIZE}MB
LROOT      ${node}p6  : ${LROOT_SIZE}MB
LAPPS      ${node}p7  : ${LAPPS_SIZE}MB
DATA       ${node}p8  : ${DATA_SIZE}MB
SYSTEM     ${node}p9  : ${SYSTEM_SIZE}MB
CACHE      ${node}p10 : ${CACHE_SIZE}MB
DEVICE     ${node}p11 : ${DEVICE_SIZE}MB
MISC       ${node}p12 : ${MISC_SIZE}MB
DATAFOOTER ${node}p13 : ${DATAFOOTER_SIZE}MB
METADATA   ${node}p14 : ${METADATA_SIZE}MB
=============================
EOF

if [ "${cal_only}" -eq "1" ]; then
exit
fi

function format_android
{
    echo "formating android data partition"
	dd if=/dev/zero of=${node}p8 bs=1M count=4
	sync
    mkfs.ext4 -E nodiscard -b 4096 -m 0 -O ^flex_bg,^uninit_bg ${node}p8 -L data
	echo "formating android/linux virtual sdcard partition"
	mkfs.vfat -F 32 ${node}p4
}

# destroy the partition table
dd if=/dev/zero of=${node} bs=1024 count=1

sfdisk --force ${opt_unit} ${node} << EOF
,${boot_sizeb}${unit_mb},83
,${ABOOT_SIZE}${unit_mb},83
,${extend_size}${unit_mb},5
,${disk_size}${unit_mb},c
,${RBOOT_SIZE}${unit_mb},83
,${LROOT_SIZE}${unit_mb},83
,${LAPPS_SIZE}${unit_mb},83
,${DATA_SIZE}${unit_mb},83
,${SYSTEM_SIZE}${unit_mb},83
,${CACHE_SIZE}${unit_mb},83
,${DEVICE_SIZE}${unit_mb},83
,${MISC_SIZE}${unit_mb},83
,${DATAFOOTER_SIZE}${unit_mb},83
,${METADATA_SIZE}${unit_mb},83
EOF


sfdisk --force ${opt_unit} ${node} -N1 << EOF
${UBOOT_SIZE}${unit_mb},${LBOOT_SIZE}${unit_mb},83
EOF


format_android


# For MFGTool Notes:
# MFGTool use mkemmc-LLAA.tar store this script
# if you want change it.
# do following:
#   tar xf mkemmc-LLAA.sh.tar
#   vi mkemmc-LLAA.sh 
#   [ edit want you want to change ]
#   rm mksdcard-android.sh.tar; tar cf mkemmc-LLAA.sh.tar mkemmc-LLAA.sh

